package com.Rolin.wss.service.impl;

import com.Rolin.wss.dao.MealMapper;
import com.Rolin.wss.model.JspToController.ChooseProO;
import com.Rolin.wss.model.MealOrderVO;
import com.Rolin.wss.model.MealVO;
import com.Rolin.wss.service.MealService;
import com.Rolin.wss.util.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class MealServiceImpl implements MealService {

    //注入DAO
    @Autowired
    private MealMapper mealMapper;

    @Override
    public ArrayList<MealVO> getMealList(int type) {
        return mealMapper.selectMealsByType(type);
    }

    @Override
    public MealOrderVO getOrderedAMeal(int ticketID) {
        return mealMapper.selectMealOrderByTicket(ticketID);
    }

    @Override
    public boolean buyFreeMeal(int userID, int ticketID, int food) {
        if(mealMapper.selectMealOrderByTicket(ticketID) == null){ //不存在该订单
            return mealMapper.insertFreeMeal(userID,ticketID,food, utils.getTimeNow())==1;
        }else{ //存在订单
            return mealMapper.updateFreeMeal(ticketID,food,utils.getTimeNow())==1;
        }
    }

    @Override
    public MealVO getMeal(int food) {
        return mealMapper.selectMealByID(food);
    }

    @Override
    public boolean buyProMeal(ChooseProO cpo) {
        if(mealMapper.selectMealOrderByTicket(cpo.getTicketID()) == null){ //不存在该订单
            return mealMapper.insertProMeal(cpo.getUserID(),cpo.getTicketID(),cpo.getFood1(),cpo.getFood2(),cpo.getFood3(),utils.getTimeNow()) == 1;
        }else{ //存在订单
            return mealMapper.updateProMeal(cpo.getTicketID(), cpo.getFood1(), cpo.getFood2(), cpo.getFood3(),utils.getTimeNow())==1;
        }
    }
}
